import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/view/loading_view.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:love_newf/app/com/model/friend_user_info_model.dart';
import 'package:love_newf/app/com/model/room_type.dart';
import 'package:love_newf/app/com/view/label/province_label.dart';
import 'package:love_newf/app/com/view/label/sex_label.dart';
import 'package:love_newf/app/com/view/label/vip_grade_label.dart';
import 'package:love_newf/value/app_colors.dart';
import 'package:love_newf/value/app_const.dart';

import 'friend_list_logic.dart';

class FriendListPage extends StatefulWidget {
  const FriendListPage({Key? key}) : super(key: key);

  @override
  State<FriendListPage> createState() => _FriendListPageState();
}

class _FriendListPageState extends State<FriendListPage> {
  final FriendListLogic logic = Get.put(FriendListLogic());

  @override
  Widget build(BuildContext context) {
    return LContainer(child: GetBuilder<FriendListLogic>(builder: (L) => body))
        .bgColor(Colors.white);
  }

  get body => LoadingView(
        datas: logic.datas,
        itemCall: (context, position) => itemView(position),
        isPullUp: true,
        isPullDown: true,
        onStateClick: (type) {
          if ((logic.datas?.length ?? 0) == 0) logic.datas = null;
          logic.update();
        },
        onLoad: (type) async {
          if (type == 1) {
            logic.pageNum = 1;
          } else if (type == 2) {
            logic.pageNum++;
          }
          return await logic.getData();
        },
        onSuccess: () {
          if (logic.datas != null) logic.update();
        },
      );

  itemView(int position) {
    FriendUserInfoModel item = logic.datas![position];

    var avatarV = LImage.easy(item.avatar, 66.w, 66.w, 66.w);
    if (item.online == 1) {
      avatarV.border(1.w, AppColors.primaryColor);
    }
    return LRow.eazy([
      avatarV,
      12.lsw,
      LColumn.eazy([
        LText.easy(item.nick, 14.sp, fw5),
        4.lsh,
        LRow.eazy([
          SexAgeLabel(item.sex, item.age),
          4.lsw,
          ProvinceLabel(item.province),
          4.lsw,
          VipGradeLabel(item.loversGrade)
        ]),
        11.lsh,
        LText.easy(item.introduction, 12.w, null, const Color(0xFFAAAAA8)),
      ]).align(Alignment.topLeft).flex(1),
      if (item.living == 1)
        LText.easy(RoomType.name(item.roomType), 11.w, fw6, Colors.white)
            .gradient(RoomType.liveTextGr(item.roomType), Alignment.centerRight)
    ]).margin(bottom: 24.w).paddingH(15.w);
  }

  @override
  void dispose() {
    Get.delete<FriendListLogic>();
    super.dispose();
  }
}
